package com.tebiecloud.goods.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tebiecloud.act.client.constants.ResultEnum;
import com.tebiecloud.common.utils.RedisUtils;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.common.utils.UUIDUtil;
import com.tebiecloud.goods.client.model.entity.GoodsSpecification;
import com.tebiecloud.goods.server.mapper.GoodsSpecificationMapper;
import com.tebiecloud.goods.server.service.GoodsSpecificationService;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * 商品的规格信息 服务实现类
 * @author tebie
 * @date 2019-09-12
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GoodsSpecificationServiceImpl extends BaseServiceImpl<GoodsSpecificationMapper, GoodsSpecification> implements GoodsSpecificationService {

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;

    @Override
    public Map<String, String> updateStockQty(long goodsSpecificationId, int amount) {
        Map<String, String> resultMap = new HashMap<>();
        int time =  5;
        String key = String.valueOf(goodsSpecificationId);
        boolean isLock = redisUtils.lock(key,time);
        if(!isLock){
            throw new RuntimeException("库存被占用，请稍等");
        }
        QueryWrapper<GoodsSpecification> goodsSpecificationQueryWrapper = new QueryWrapper<>();
        goodsSpecificationQueryWrapper.eq("spec_id",goodsSpecificationId);
        goodsSpecificationQueryWrapper.eq("deleted",0);
        GoodsSpecification goodsSpecificationOld = goodsSpecificationMapper.selectOne(goodsSpecificationQueryWrapper);
        //不更新库存
        if(goodsSpecificationOld.getStockQty()!=null &&  goodsSpecificationOld.getStockQty() == -99){
            //解锁
            redisUtils.unLock(key);
            resultMap.put("status","OK");
            return resultMap;
        }
        //计算库存
        int currentAmount = goodsSpecificationOld.getStockQty() + amount;
        if(currentAmount<0){
            //解锁
            redisUtils.unLock(key);
            resultMap.put("status","error");
            resultMap.put("message", ResultEnum.ACT_STOCKQTY_ERROR.getMessage());
            return resultMap;
        }
        UpdateWrapper<GoodsSpecification> goodsSpecificationUpdateWrapper = new UpdateWrapper<>();
        goodsSpecificationUpdateWrapper.eq("spec_id",goodsSpecificationId);
        goodsSpecificationUpdateWrapper.set("stock_qty",currentAmount);
        GoodsSpecification goodsSpecification = new GoodsSpecification();
        goodsSpecification.setStockQty(currentAmount);
        goodsSpecificationMapper.update(goodsSpecification,goodsSpecificationUpdateWrapper);
        //解锁
        redisUtils.unLock(key);
        resultMap.put("status","OK");
        return resultMap;
    }

    @Override
    public Map<String, String> updateSalesVolume(long goodsSpecificationId, int salesVolume) {
        Map<String, String> resultMap = new HashMap<>();
        int time =  10;
        String key = String.valueOf(goodsSpecificationId);
        boolean isLock = redisUtils.lock(key,time);
        if(!isLock){
            throw new RuntimeException("销量被占用，请稍等");
        }
        QueryWrapper<GoodsSpecification> goodsSpecificationQueryWrapper = new QueryWrapper<>();
        goodsSpecificationQueryWrapper.eq("spec_id",goodsSpecificationId);
        goodsSpecificationQueryWrapper.eq("deleted",0);
        GoodsSpecification actGoodsRef = goodsSpecificationMapper.selectOne(goodsSpecificationQueryWrapper);
        //计算库存
        int currentSalesVolume = actGoodsRef.getSalesVolume() + salesVolume;
        UpdateWrapper<GoodsSpecification> goodsSpecificationUpdateWrapper = new UpdateWrapper<>();
        goodsSpecificationUpdateWrapper.eq("spec_id",goodsSpecificationId);
        goodsSpecificationUpdateWrapper.set("sales_volume",currentSalesVolume);
        GoodsSpecification goodsSpecification = new GoodsSpecification();
        goodsSpecification.setSalesVolume(currentSalesVolume);
        goodsSpecificationMapper.update(goodsSpecification,goodsSpecificationUpdateWrapper);
        //解锁
        redisUtils.unLock(key);
        resultMap.put("status","OK");
        return resultMap;
    }
}
